package org.benjamin.lending.controller;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean;
import org.springframework.stereotype.Controller;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.RedirectView;
import org.springframework.web.util.WebUtils;

@Controller
@RequestMapping(value = "sumapayTest")
public class SumapayTestController {
	
	private Logger logger = LogManager.getLogger();
	
	@Autowired
	private RestTemplate restTemplate;
	
	@PostMapping(value = "/user/register_toRegister")
	public ModelAndView userRegisterToRegister(HttpServletRequest servletRequest) {
		logger.info("-----------------------------------测试--------------------------------");
		ModelAndView modelAndView = new ModelAndView();
		Map<String,Object> requestMap = WebUtils.getParametersStartingWith(servletRequest, "");
		logger.info("模拟测试{}",requestMap);
		MultiValueMap<String, String> responseMap = new LinkedMultiValueMap<String, String>();
		for(String key : requestMap.keySet()) {
			responseMap.add(key, requestMap.get(key).toString());	
		}
		responseMap.add("result", "00000");
		logger.info("发送异步数据响应");
		String result = this.restTemplate.postForObject(requestMap.get("noticeUrl").toString(), responseMap, String.class);
		if(result.equals("success")) {
			
		}
		String successReturnUrl = requestMap.get("successReturnUrl").toString();
		RedirectView redirectView = new RedirectView(successReturnUrl);
		modelAndView.setView(redirectView);
		return modelAndView;
	}

}
